/**
 * GrayFilter.java
 * 
 * @date: 2012-03-14
 * @author: Xiaoyu Guo (MUN# 200982155)
 * This file is part of the course project for COMP 6752, 
 * Application of Computer Graphics. All the file is written
 * by myself.
 */
package mun.cs6752.project.filter.gray;

import java.awt.image.BufferedImage;

import mun.cs6752.project.filter.abstractClasses.AbstractImageFilter;
import mun.cs6752.project.filter.abstractClasses.AbstractImageFilterControl;
import mun.cs6752.project.filter.interfaces.IImageFilterParameter;
import mun.cs6752.project.filter.interfaces.IImageProvider;

/** Gray Filter simply drops all the color information
 * @author Xiaoyu Guo
 *
 */
public class GrayFilter extends AbstractImageFilter {

    /**
     * @param provider
     * @param name
     */
    public GrayFilter(IImageProvider provider) {
        super(provider, "Gray");
    }

    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilter#execute(java.awt.image.BufferedImage, mun.cs6752.project.filter.interfaces.IImageFilterParameter)
     */
    @Override
    public BufferedImage execute(BufferedImage srcImage, IImageFilterParameter param) {
        BufferedImage result = new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        for(int y = 0; y < srcImage.getHeight(); y++){
            for(int x = 0; x < srcImage.getWidth(); x++){
                int rgb = srcImage.getRGB(x, y);
                int r = getR(rgb);
                int g = getG(rgb);
                int b = getB(rgb);
                int average = (r + g + b) / 3;
                rgb = getRGB(average, average, average);
                result.setRGB(x, y, rgb);
            }
        }
        
        return result;
    }

    /* (non-Javadoc)
     * @see mun.cs6752.project.filter.interfaces.IImageFilter#getControl()
     */
    @Override
    public AbstractImageFilterControl getControl() {
        return new AbstractImageFilterControl(provider, this){
            private static final long serialVersionUID = 1209572214313451691L;

            @Override
            public IImageFilterParameter getParameter() {
                return null;
            }
        };
    }

}
